<?php
class Member{
    public $time;
    public $game_type = 'hjsg';
    public $bzws_url;
    public $token_uid;
    public $token;
    public function __construct()
    {
        $this->time = time();
    }
    //curl_post
    private function post($url, $data, $header = [], $sleep = 1000000, $i = 1)
    {
        if ($sleep > 0) {
            usleep($sleep);
        }

        if (!$header) {
            $header = array(
                "Accept: application/json",
                "Accept-Encoding: gzip, deflate",
                "Accept-Language: zh-CN,zh;q=0.9",
                "Connection: keep-alive",
                "Content-Type: application/x-www-form-urlencoded",
                "Origin: http://sgqyz.linzhigame.com",
                "Referer: http://sgqyz.linzhigame.com/index.html",
                "User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36"
            );
            //暴走的
            if ($this->game_type == 'bzws') {
                $header = array(
                    "Accept: application/json",
                    "Accept-Encoding: gzip, deflate",
                    "Accept-Language: zh-CN,zh;q=0.9",
                    "Connection: keep-alive",
                    "Content-Type: application/x-www-form-urlencoded",
                    "Origin: http://game.bzws.zhanshunkeji.com",
                    "Referer: http://game.bzws.zhanshunkeji.com/",
                    "User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36"
                );
            }
        }
        $data2 = $data;
        foreach ($data as $k => $v) {
            $data[$k] = urlencode($v);
        }
        $data = http_build_query($data);
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        curl_setopt($ch, CURLOPT_ACCEPT_ENCODING, "gzip,deflate");
        $s = curl_exec($ch);
        curl_close($ch);
        $res = json_decode($s, 1);
        if ($res == 403 || !$res) {
            echo json_encode([
                'code' => -1,
                'url' => $url,
                'msg' => $res['msg']??'403请重试',
                'data' => $res
            ]);die;
        } elseif ($res['status'] == 11) {
            $res = $this->post($url, $data2, [], 2000000 * $i, $i + 2);
        }
        return $res;
    }

    //url参数转换
    private function transformation_url($url, $token = '')
    {
        if ($token) {
            $d = date('Y');
            $c = date('n');
            $b = date('j');
            $a = date('w');
            if ($this->game_type == 'bzws') {
                $sign = 'a4c13b9ecf81';
            } else {
                $sign = ceil(($d + $c + $b) * pow($a + 1, $a + 1));
            }
            $signature = md5(md5($token . $this->time) . $sign);
        }
        $url = $this->bzws_url . "?" . http_build_query($url) . '&channel=150&lang=zh-cn&v=0&rand=' . $this->time . '&signature=' . $signature;
        return $url;
    }
    private function login0($u, $p, $q)
    {
        $d = date('Y');
        $c = date('n');
        $b = date('j');
        $a = date('w');
        if ($this->game_type == 'bzws') {
            $sign = 'a4c13b9ecf81';
        } else {
            $sign = ceil(($d + $c + $b) * pow($a + 1, $a + 1));
        }
        $signature = md5(md5($this->time) . $sign);
        $url = 'http://uc.game.zhanchenggame.com/index.php?&c=user&m=login&&token=&channel=150&lang=zh-cn&rand=' . $this->time . '&signature=' . $signature;
        //暴走的
        if ($this->game_type == 'bzws') {
            $url = 'http://bzws-uc.game.zhanyougame.com/index.php?&c=user&m=login&&token=&channel=9&lang=zh-cn&rand=' . $this->time . '&signature=';
        }
        $arr['u'] = $u;
        $arr['p'] = $p;
        $arr['mac'] = '00:00:00:00:00:00';
        $arr['devicetoken'] = '000000';
        $arr['channel'] = '150';

        $res = $this->post($url, $arr, [], 0);
        if ($res['status'] == 1) {
            $this->token_uid = $res['uid'];
        } else {
            echo json_encode([
                'code' => -1,
                'msg' => '登录失败1',
                'data' => []
            ]);die;
        }
        //echo die;
    }

    //登录
    private function login($u, $p, $q)
    {
        $this->bzws_url = 'http://s' . $q . '.game.zhanchenggame.com/index.php';
        //暴走的
        if ($this->game_type == 'bzws') {
            $this->bzws_url = 'http://bzws-s' . $q . '.game.zhanyougame.com/index.php';
        }
        $c['c'] = 'login';
        $c['m'] = 'user';
        $url = $this->transformation_url($c, '');
        $arr['u'] = $u;
        $arr['p'] = $p;
        $arr['mac'] = '00:00:00:00:00:00';
        $arr['devicetoken'] = '000000';
        $arr['channel'] = '150';
        $res = $this->post($url, $arr, [], 0);
        if ($res['status'] == 1) {
            $this->token = $res['token'];
        } else {
            echo json_encode([
                'code' => -1,
                'msg' => '登录失败',
                'data' => []
            ]);die;
        }
    }
    //获取用户信息
    private function member()
    {
        $c['c'] = 'member';
        $c['m'] = 'index';
        $c['token_uid'] = $this->token_uid;
        $c['token'] = $this->token;
        $url = $this->transformation_url($c, $this->token);
        $arr['token'] = $this->token;
        $res = $this->post($url, $arr, [], 0);
        if ($res['uid']) {
            return $res;
        } else {
            echo json_encode([
                'code' => -1,
                'msg' => '获取用户信息失败',
                'data' => []
            ]);die;
        }
    }
    //获取进阶信息
    private function advanced()
    {
        $c['c'] = 'advanced';
        $c['m'] = 'index';
        $c['token_uid'] = $this->token_uid;
        $c['token'] = $this->token;
        $url = $this->transformation_url($c, $this->token);
        $arr['token'] = $this->token;
        $res = $this->post($url, $arr, [], 0);
        if ($res['status'] == 1) {
            return $res['resource'];
        } else {
            return [];
        }
    }
    //获取奇门信息
    private function chimen()
    {
        $c['c'] = 'chimen';
        $c['m'] = 'index';
        $c['token_uid'] = $this->token_uid;
        $c['token'] = $this->token;
        $url = $this->transformation_url($c, $this->token);
        $arr['token'] = $this->token;
        $res = $this->post($url, $arr, [], 0);
        if ($res['status'] == 1) {
            return [
                'ziyingshi' => $res['advance']['my_goods_1'],
                'zhangong' => $res['advance']['my_goods_4'],
                'meigui' => $res['advance']['my_goods_5'],
                'zuanshi' => $res['advance']['my_goods_6'],
                'jade1' => $res['blue_jade'],
            ];
        } else {
           return [];
        }
    }
    //获取精炼信息
    private function refine()
    {
        $c['c'] = 'refine';
        $c['m'] = 'index';
        $c['token_uid'] = $this->token_uid;
        $c['token'] = $this->token;
        $url = $this->transformation_url($c, $this->token);
        $arr['token'] = $this->token;
        $res = $this->post($url, $arr, [], 0);
        if ($res['status'] == 1) {
            return $res['resource'];
        } else {
           return [];
        }
    }
    public function run($member_text = ''){
        if (!$member_text) {
            echo json_encode([
                'code' => -1,
                'msg' => '请选择账号文件',
                'data' => []
            ]);die;
        }
        if ($_POST['game_type'] == 'bzws') {
            $this->game_type = 'bzws';
        }
        $users = explode("\n", $member_text);
        $num = $_POST['index'] ?? 1;
        $num = min($num,count($users) - 1);
        $user = explode(',', $users[$num]);
        $this->login0($user[0], $user[1], (int)$user[2]);
        $this->login($user[0], $user[1], (int)$user[2]);
        $info = $this->member();
        $advanced = $this->advanced();
        $chimen = $this->chimen();
        $refine = $this->refine();
        $info = array_merge($info, $advanced, $chimen, $refine);
        $info['zhanghao'] = $user[0];
        $info['jade3'] = $this->formatNumber($info['jade3']);
        $info['jade2'] = $this->formatNumber($info['jade2']);
        $info['jade1'] = $this->formatNumber($info['jade1']);
        $info['silver'] = $this->formatNumber($info['silver']);
        $info['reputation'] = $this->formatNumber($info['reputation']);
        $info['zhangong'] = $this->formatNumber($info['zhangong']);
        $info['daofu'] = $this->formatNumber($info['daofu']);
        $info['soul'] = $this->formatNumber($info['soul']);
        $info['jade'] = $this->formatNumber($info['jade']);
        $info['refinestone'] = $this->formatNumber($info['refinestone']);
        $info['colorstone'] = $this->formatNumber($info['colorstone']);
        $info['bloodstone'] = $this->formatNumber($info['bloodstone']);
        echo json_encode([
            'code' => 1,
            'msg' => '',
            'data' => $info
        ]);die;
    }

    private function formatNumber($num)
    {
        if(!is_numeric($num)){
            return '0';
        }
        if($num / 100000000 > 1){
            return round($num / 100000000, 2).'亿';
        }
        if($num / 10000 > 1){
            return round($num / 10000, 2).'万';
        }
        return $num;
    }
}

$Member = new Member();
$Member->run($_POST['member_text']);